package com.neuedu.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 金山
 * 项目：mybatis-learn
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/49:31
 */
public class MyBatisUtils {

    private static SqlSessionFactory sqlSessionFactory;


    /**
     * 用于创建  SqlSessionFactory
     * @param resource  主配置文件
     * @return
     */
    public synchronized static SqlSessionFactory getSqlSessionFactory(String  resource){

        if(sqlSessionFactory == null){
            try {
                InputStream inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

        return sqlSessionFactory;

    }
}
